import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ProducerConsumerModel {

    public static void main(String[] args) {
        MyBlockingQueueClass queue=new MyBlockingQueueClass(1000);
        //生产者线程
        Thread t1=new Thread(()-> {
            int i=1;
            while(true) {
                try {
                    queue.put(""+i);
                    System.out.println("生产元素"+i);
                    i++;
                    //给生产操作加上sleep,减缓生产速度
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        //消费者线程
        Thread t2=new Thread(()-> {
            while(true) {
                try {
                    Integer i=Integer.parseInt(queue.take());//架构String类型转换为Integer类型
                    System.out.println("消费元素"+i);
                    //给消费操作加上sleep,生产快点,消费慢点
//                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
    public static void main1(String[] args) {
        BlockingQueue<Integer> queue=new ArrayBlockingQueue<Integer>(1000);
        //生产者线程
        Thread t1=new Thread(()-> {
            int i=1;
            while(true) {
                try {
                    queue.put(i);
                    System.out.println("生产元素"+i);
                    i++;
                    //给生产操作加上sleep,减缓生产速度
//                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        //消费者线程
        Thread t2=new Thread(()-> {
            while(true) {
                try {
                    Integer i=queue.take();
                    System.out.println("消费元素"+i);
                    //给消费操作加上sleep,生产快点,消费慢点
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
